#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include "namepipe.hpp"

int main()
{
    key_t key = ftok("/home/fwt/lstudy/SharedMemory", 6686);
    int shmid = shmget(key, 4096, IPC_CREAT | IPC_EXCL | 0666);
    char *addr = (char *)shmat(shmid, nullptr, 0);
   // if((int)addr == -1 )perror("shmat:");

    namepipe pipe(pipespace::gpathname, pipespace::gCreat);
    pipe.OpenPipeWrite();  
    std::string buffer = "ok";
   
    for( int i = 0; i <= 5; i++)
    {
        pipe.DeliverMassage(buffer);
        sprintf(addr, " %d times : I am process A \n", i );
        printf("Process A deliver : %s", addr);
        sleep(1);
    }
    pipe.DeliverMassage(buffer);
    sprintf(addr, "I am process A , i will quit \n");
    printf("Process A deliver : %s", addr);

    shmdt(addr);
    shmctl(shmid, IPC_RMID, nullptr);
}